package com.apotik.legenda.custom;

import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class TableModelCustom  extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	
	private String[] columnNames;
	
	@SuppressWarnings("rawtypes")
	private Vector rowVector = new Vector();
	
	
	public TableModelCustom(String[] columnNames) {
		this.columnNames = columnNames;
	}


	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		return rowVector.size();
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Object getValueAt(int rowIndex, int colIndex) {
		Vector v = (Vector) rowVector.get(rowIndex);
		return v.get(colIndex);
	}

	
	@Override
	public String getColumnName(int col) {
		return columnNames[col];
	}
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		Vector v = (Vector) rowVector.get(0);
		return v.get(columnIndex).getClass();
	}
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void insertData(Vector v) {
		rowVector.add(v);	
		fireTableDataChanged();
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void updateData(int rowIndex, Vector value){
		Vector v = (Vector) rowVector.get(rowIndex);
		rowVector.remove(rowIndex);
		rowVector.add(rowIndex, value);
		for(int i=0;i<v.size();i++){
			fireTableCellUpdated(rowIndex, i);
		}
	}

	public void removeRow(int row) {
		rowVector.remove(row);
		fireTableDataChanged();
	}
	
	public void removeRowAll(){
		rowVector.removeAllElements();
		fireTableDataChanged();
	}
	
	
	@SuppressWarnings("rawtypes")
	public Vector getRowVector() {
		return rowVector;
	}
	
	/*@SuppressWarnings("rawtypes")
	public void setRowVector(Vector rowVector) {
		this.rowVector = rowVector;
	}*/
	
	
	
	
}

